package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientInfoMapper;
import com.leyou.auth.service.ClientInfoService;
import com.leyou.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ClientInfoServiceImpl extends ServiceImpl<ClientInfoMapper,ClientInfo> implements ClientInfoService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${ly.jwt.key}")
    private String key;

    @Override
    public String getSecretKey(String clientId, String secret) {

        ClientInfo client_id = query().eq(null != clientId, "client_id", clientId).one();

        if (client_id == null) {
            throw new LyException(401,"用户不存在");
        }
        if(!passwordEncoder.matches(secret, client_id.getSecret())){
            throw new LyException(401, "账号和密码不匹配");
        }

        return key;


    }
}
